//
//  RootViewController.m
//  UI_demo_SearchBar
//
//  Created by dllo on 15/12/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating>

@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) UISearchController *searchC;
@property (nonatomic, retain) NSMutableArray *tableArr;
@property (nonatomic, retain) NSMutableArray *searchResults;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    
    self.searchC = [[UISearchController alloc] initWithSearchResultsController:nil];
    // 搜索时是否显示背景
    self.searchC.dimsBackgroundDuringPresentation = NO;
    // 设置显示搜索结果控制器
    self.searchC.searchResultsUpdater = self;
    // 设置搜索栏大小
    self.searchC .searchBar.frame = CGRectMake(0, 5, [UIScreen mainScreen].bounds.size.width, 44);
    // 将searchBar添加到tableView里
    self.tableV.tableHeaderView = self.searchC.searchBar;
    
    self.tableArr = [NSMutableArray array];
    
    for (NSInteger i = 0; i < 200; i++) {
        NSString *str = [NSString stringWithFormat:@"%ld啦啦啦", (NSInteger)arc4random() % 100000];
        [self.tableArr addObject:str];
    }
    // 搜索结果开空间
//    self.searchResults = [NSMutableArray array];
    
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *str = self.searchC.searchBar.text;
    // 搜索核心 相当于SQL里面的WHERE
    NSPredicate *predicata = [NSPredicate predicateWithFormat:@"self contains[c] %@", str];
    if (0 != self.searchResults.count) {
        [self.searchResults removeAllObjects];
    }
    self.searchResults = [NSMutableArray arrayWithArray:[self.tableArr filteredArrayUsingPredicate:predicata]];
    
    [self.tableV reloadData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchC.active) {
        return self.searchResults.count;
    } else {
        return self.tableArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    if (self.searchC.active) {
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [self.tableArr objectAtIndex:indexPath.row];
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
